package dao;

import dataBase.goodsData;

import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

public class goodsDao {
    public ArrayList<goodsData> goodsList = new ArrayList<>();

    public goodsDao(){
        addgood1();
        addgood2();
        addgood3();
    }

    public void addgood1() {
        goodsData goods = new goodsData();
        int ID0 = 0;
        String name0 = "book";
        float price0 = 5.2f;
        goods.setGoodsId(ID0);
        goods.setName(name0);
        goods.setPrice(price0);
        goodsList.add(goods);
    }

    public void addgood2() {
        goodsData goods = new goodsData();
        int ID1 = 1;
        String name1 = "eraser";
        float price1 = 2.6f;
        goods.setGoodsId(ID1);
        goods.setName(name1);
        goods.setPrice(price1);
        goodsList.add(goods);
    }

    public void addgood3() {
        goodsData goods = new goodsData();
        int ID2 = 2;
        String name2 = "pen";
        float price2 = 9.6f;
        goods.setGoodsId(ID2);
        goods.setName(name2);
        goods.setPrice(price2);
        goodsList.add(goods);
    }

    public void listGoodsData() {
        System.out.println("编号\t\t价钱\t\t名称");
        for (goodsData goodsData : goodsList) {
            System.out.println(goodsData.showGoodsData());
        }
        System.out.println("\n");
    }

    public boolean addGoods(int id,String name,float price) {
        goodsData goods = new goodsData();
        for (goodsData good : goodsList) {
            if (Objects.equals(good.getGoodsId(), id)) {
                return false;
            }
        }
        goods.setGoodsId(id);
        goods.setName(name);
        goods.setPrice(price);
        goodsList.add(goods);
        return true;
    }

    public boolean checkGoods() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择查询方式 1：编号 2：名称");
        int num = sc.nextInt();
        if ((num == 1)) {
            System.out.println("请输入查询编号");
            int id = sc.nextInt();
            for (goodsData good : goodsList) {
                if (Objects.equals(good.getGoodsId(), id)) {
                    String str = good.showGoodsData();
                    System.out.println("编号\t\t价钱\t\t名称");
                    System.out.println(str);
                    System.out.println("\n");
                    return true;
                }
            }
        } else {
            String name = sc.next();
            for (goodsData good : goodsList) {
                if (Objects.equals(good.getName(), name)) {
                    String str = good.showGoodsData();
                    System.out.println("编号\t\t价钱\t\t名称");
                    System.out.println(str);
                    System.out.println("\n");
                    return true;
                }
            }
        }
        return false;
    }

    public boolean setGoodsAttribute() {
        listGoodsData();
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择查询方式 1：编号 2：名称");
        int num = sc.nextInt();
        if ((num == 1)) {
            System.out.println("请输入查询编号");
            int id = sc.nextInt();
            for (goodsData good : goodsList) {
                if (Objects.equals(good.getGoodsId(), id)) {
                    System.out.println("请输入要修改的属性");
                    System.out.println("1，名称");
                    System.out.println("2，价钱");
                    int choose = sc.nextInt();
                    if (choose == 1) {
                        System.out.println("请输入新商品名");
                        String newName = sc.next();
                        good.setName(newName);
                        System.out.println("商品列表已更新");
                        listGoodsData();
                        return true;
                    } else if (choose == 2) {
                        System.out.println("请输入新商品价钱");
                        float newPrice = sc.nextFloat();
                        good.setPrice(newPrice);
                        System.out.println("商品列表已更新");
                        listGoodsData();
                        return true;
                    }
                }
            }
        } else if (num == 2) {
            String name = sc.next();
            for (goodsData good : goodsList) {
                if (Objects.equals(good.getName(), name)) {
                    System.out.println("请输入要修改的属性");
                    System.out.println("1，名称");
                    System.out.println("2，价钱");
                    int choose = sc.nextInt();
                    if (choose == 1) {
                        System.out.println("请输入新商品名");
                        String newName = sc.next();
                        good.setName(newName);
                        System.out.println("商品列表已更新");
                        listGoodsData();
                        return true;
                    } else if (choose == 2) {
                        System.out.println("请输入新商品价钱");
                        float newPrice = sc.nextFloat();
                        good.setPrice(newPrice);
                        System.out.println("商品列表已更新");
                        listGoodsData();
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public boolean delGoods(String delName) {
        for (goodsData good : goodsList) {
            if (Objects.equals(good.getName(), delName)) {
                goodsList.removeIf(goods -> Objects.equals(goods.getName(), delName));
                System.out.println("商品列表已更新");
                listGoodsData();
                return true;
            }
        }
        return false;
    }

    public boolean setDiscount(int num,float count) {
        for (goodsData goods : goodsList) {
            if(Objects.equals(num, goods.getGoodsId())) {
                    goods.setFlog(true);
                    goods.setPrice(count * goods.getPrice());
                    return true;
            }
        }
        return false;
    }

    public void checkDiscount() {
        for (goodsData goods : goodsList) {
            if (goods.getFlog()) {
                System.out.println(goods.showGoodsData());
            }
        }
    }
}
